<?php

require FCPATH.'branch/fqb/D_Home_Form.php';

class Form_wenjuan extends D_Home_Form {

	public function __construct() {
		parent::__construct();
	}

    public function index() {


        if (IS_POST) {
            // 验证码
            $post = $this->input->post('data', TRUE);
            $wj = dr_string2array($this->cdata['wjnr']);
            if (!$wj) {
                exit(dr_json(0, '问卷表未录入数据'));
            } elseif ($this->cdata['yxq'] && $this->cdata['yxq'] - SYS_TIME < 0) {
                exit(dr_json(0, '问卷已经过期，不能进行提交'));
            }

            $name = md5($this->uid.dr_now_url());
            if ($this->session->userdata($name)) {
                // 发布间隔判断
                exit(dr_json(0, fc_lang('您动作太快了')));
            }

            foreach ($wj as $i => $t) {
                if (!isset($post[$i])) {
                    exit(dr_json(0, '【'.$t['name'].'】未填写或选择'));
                }
            }
            $data = array();
            $data[1]['cid'] = $this->cid;
            $data[1]['uid'] = $this->uid;
            $data[1]['url'] = $this->cdata['url'];
            $data[1]['title'] = $this->cdata['title'];
            $data[1]['author'] = $this->member['username'] ? $this->member['username'] : 'guest';
            $data[1]['inputip'] = $this->input->ip_address();
            $data[1]['inputtime'] = SYS_TIME;
            $data[1]['subject'] = '';
            $data[1]['content'] = dr_array2string($post);
            if ($id = $this->_add($data) && $this->uid) {
                $rule = $this->form['permission'][$this->markrule];
                // 积分处理
                $rule['experience'] && $this->member_model->update_score(0, $this->uid, $rule['experience'], '', $this->form['name']);
                // 虚拟币处理
                $rule['score'] && $this->member_model->update_score(1, $this->uid, $rule['score'], '', $this->form['name']);
                // 操作成功处理附件
                $this->attachment_handle($this->uid, $this->db->dbprefix($this->table).'-'.$id, $this->form['field']);
            }
            $this->session->set_tempdata($name, 1, 30);
            exit(dr_json(1, '问卷调查提交成功'));
        } else {
            $this->msg(fc_lang('问卷未提交'));
        }

    }
}